<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
    <style>
        .menudiv .layui-input-inline,.buttondiv .layui-input-inline {
            float: left;
            width: 450px !important;
            margin-right: 10px;
        }
        .buttondiv{
            display: none;
        }
        .layui-form-item{
            width: 700px;
        }
        .error{
            color: red;
            line-height: 20px;
        }

        input.error, select.error {
            background-color: #fbe2e2;
            border-color: #c66161;
            color: #c00;
        }

    </style>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground right" style="float: left;width:65%; padding-left:0%;margin-top: 3%">
        <div class="menudiv">
            <form class="layui-form"  id="menuform" >


                <div class="layui-form-item">
                    <label class="layui-form-label">父&emsp;&emsp;级</label>
                    <div class="layui-input-inline">
                        <input type="text"  id="parentName"  autocomplete="off" readonly="readonly"  class="layui-input">
                        <input type="hidden" id="parentId" name="parentId">
                        <input type="hidden" id="hierarchy" name="hierarchy">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">专&emsp;&emsp;业</label>
                    <div class="layui-input-inline">
                        <input type="text"  id="disciplinesName"  autocomplete="off" readonly="readonly"  class="layui-input">
                        <input type="hidden" id="disciplinesId" name="disciplinesId">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">名&emsp;&emsp;称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" autocomplete="off" placeholder="请输入名称" class="layui-input">
                    </div>
                </div>



                <div class="layui-form-item">
                    <label class="layui-form-label">EBS编码</label>
                    <div class="layui-input-inline">
                        <input type="text" id="ebs" name="ebs" autocomplete="off" placeholder="请输入EBS编码" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">单&emsp;&emsp;位</label>
                    <div class="layui-input-inline">
                        <input type="text" id="unit" name="unit" autocomplete="off" placeholder="请输入单位" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">IFD&ensp;编码</label>
                    <div class="layui-input-inline">
                        <input type="text" id="ifd" name="ifd" autocomplete="off" placeholder="请输入IFD编码" class="layui-input">
                    </div>
                </div>



                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">备&emsp;&emsp;注</label>
                        <div class="layui-input-inline">
                            <textarea id="comment" name="comment" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<SCRIPT type="text/javascript">
    var form;

    $(function () {
        var type=getUrlParam('type');
        //勾选的id
        var id=getUrlParam('id');
        var disciplinesName=getUrlParam('disciplinesName');
        var disciplinesId=getUrlParam('disciplinesId');
        init(type,id,disciplinesName,disciplinesId);

    });

    //初始化表单数据
    function init(type,id,disciplinesName,disciplinesId){
        $("#disciplinesName").val(disciplinesName);
        $("#disciplinesId").val(disciplinesId);

        //type 0:新增同级
        //     1:新增下级
        publicAjax (null,'resEntElementTree/queryResEntElementTreeByID',{id:id},null,null,function (r) {
            console.log(r);
            if(type==0){
                $("#parentName").val(r.data.parentName);
                $("#hierarchy").val(r.data.hierarchy);
                $("#parentId").val(r.data.parentId);
            }else{
                $("#parentName").val(r.data.name);
                $("#parentId").val(r.data.id);
                $("#hierarchy").val(r.data.hierarchy+1);
            }
        })

    }

    layui.use(['form', 'layer'], function() {
        form = layui.form
            , layer = layui.layer;

    })

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURIComponent(r[2]);
        return null;
    }

    /**
     * 保存
     */
    function submit(index, layero,type){
        if($(validform().form())[0]){
            var formData = new FormData($("#menuform")[0]);
            var result;
            publicAjaxFormData (null,'resEntElementTree/insertResEntElementTreeByID',formData,false,null,function (r) {
                result=r;
            })

            if(type=='applye'){
                if(result.code==2000){
                    laysuccess("新增成功");
                }else{
                    layerror("新增失败");
                }
            }
            $("#name").val("");
            $("#ebs").val("");
            $("#unit").val("");
            $("#ifd").val("");
            $("#comment").val("");
            return result;
        }

    }

    //编写表单验证函数validform，在验证按钮注册按钮点击事件内调用验证函数对象
    function validform(){//flag 1保存2更新;
        /*关键在此增加了一个return，返回的是一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/

        return $("#menuform").validate({
            rules : {
                name : "required",
                ebs : {
                    required:true,
                    hasEbs: true
                },
                ifd:{
                    hasIfd :true
                },
                unit : "required",
                comment: {
                    maxlength: 100
                }
            },
            messages : {
                name : {
                    required: "名称不能为空"
                },
                ebs : {
                    required: "EBS编码不能为空"
                },
                unit : {
                    required: "单位不能为空"
                },
                comment : {
                    maxlength: "备注长度不可超过100"
                }
            }
        });

    }

    jQuery.validator.addMethod("hasEbs",function(value,element){
        var data = {"ebs":value};
        var codevurl= "resEntElementTree/queryEBSCodeHasExist";
        var obj = true;
        publicAjax (null,codevurl,data,false,null,function (r) {
            if(r.code==2000){
                if(r.data>0){
                    obj = false;
                }else{
                    obj = true;
                }

            }else{
                obj = false;
            }
        })
        return obj;
    },"EBS编码已存在，请重新输入");

    jQuery.validator.addMethod("hasIfd",function(value,element){
        var data = {"ifd" : value};
        var codevurl= "resEntElementTree/queryEBSCodeHasExist";
        var obj = true;
        publicAjax (null,codevurl,data,false,null,function (r) {
            if(r.code==2000){
                if(r.data>0){
                    obj = false;
                }else{
                    obj = true;
                }

            }else{
                obj = false;
            }
        })
        return obj;
    },"IFD编码已存在，请重新输入");
</SCRIPT>
</html>